<?php
/**
 *
 * @author Tongle Xu <xutongle@gmail.com> 2013-9-14
 * @copyright Copyright (c) 2003-2103 tintsoft.com
 * @license http://www.tintsoft.com
 * @version $Id$
 */
abstract class Base_Application extends Object {

	/**
	 * 应用默认配置
	 *
	 * @var array
	 */
	protected $config = array ();

	/**
	 * 构造方法初始化
	 *
	 * @param string $config
	 */
	public function __construct($config = array()) {
		Kernel::setApp ( $this );
		$this->config = C ( 'config' );
		$this->setTimeZone ( $this->config ['timezone'] );
		mb_internal_encoding ( $this->config ['charset'] );
		define ( 'CHARSET', $this->config ['charset'] );
		define ( 'WEB_PATH', dirname ( $_SERVER ['SCRIPT_FILENAME'] ) );
	}

	/**
	 * 请求过程
	 */
	abstract public function processRequest();

	/**
	 * 运行应用程序
	 */
	public function run() {
		register_shutdown_function ( array ($this,'end' ), 0, false );
		$this->processRequest ();
	}

	/**
	 * 终止应用程序
	 *
	 * @param number $status
	 * @param string $exit
	 */
	public function end($status = 0, $exit = true) {
		if ($exit) exit ( $status );
	}

	/**
	 * 取得URL管理类实例
	 */
	public function getRouter() {
		return Kernel::getInstance ( 'Router', C ( 'router' ) );
	}

	/**
	 * 取得HttpRequest实例
	 *
	 * @return HttpRequest
	 */
	public function getRequest() {
		return Kernel::getInstance ( 'HttpRequest' );
	}

	/**
	 * 获取服务器时区
	 *
	 * @return string 时区
	 */
	public function getTimeZone() {
		return date_default_timezone_get ();
	}

	/**
	 * 设置服务器时区
	 *
	 * @param string $value 时区
	 */
	public function setTimeZone($value) {
		date_default_timezone_set ( $value );
	}
}